package com.yyfax.ph.products;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.Loader;
import com.yyfax.infrastructure.mvp.Presenter;
import com.yyfax.infrastructure.mvp.PresenterLoader;
import com.yyfax.ph.base.YJPH;
import com.yyfax.ph.common.utils.RcmPermissionHelper;
import com.yyfax.ph.home.recommendloan.RecommendPermissionModel;
import com.yyfax.ph.home.recommendloan.RecommendPermissionPresenter;
import com.yyfax.ph.personal.authority.login.LoginActivity;
import com.yyfax.ph.web.WebActivity;

public class ProductDetailActivity extends WebActivity implements RecommendPermissionPresenter.View {

	private static final int LOADER_ID = 111;
	private static final int login_requestCode = 112;
	private RecommendPermissionPresenter mRcmPermissionPresenter;

	public static void start(Activity aty, String linkUrl, String title) {
		Intent i = new Intent(aty, ProductDetailActivity.class);
		i.putExtra(WebActivity.LINKURL, linkUrl);
		i.putExtra(WebActivity.TITLE, title);
		aty.startActivity(i);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		getSupportLoaderManager().initLoader(LOADER_ID, null, this);

	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		getSupportLoaderManager().destroyLoader(LOADER_ID);
	}

	@Override
	public void onLoadFinished(Loader<Presenter> loader, Presenter data, int placeholder) {
		if (LOADER_ID == loader.getId()) {
			mRcmPermissionPresenter = (RecommendPermissionPresenter) data;
			mRcmPermissionPresenter.attach(this);
			return;
		}
		super.onLoadFinished(loader, data, placeholder);
	}

	@Override
	public Loader<Presenter> onCreateLoader(int id, Bundle args) {
		if (LOADER_ID == id) {
			return new PresenterLoader<>(this, new RecommendPermissionPresenter.Generator());
		}
		return super.onCreateLoader(id, args);

	}

	@Override
	public void goToRecommend() {
		super.goToRecommend();
		if (!YJPH.getInstance().getPreferenceManager().isLoginSuccess()) {
			LoginActivity.startActivityForResult(this, login_requestCode);
			return;
		}
		mRcmPermissionPresenter.getRcmPermission();//获取推荐权限
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (resultCode == RESULT_OK && requestCode == login_requestCode){
			mRcmPermissionPresenter.getRcmPermission();//获取推荐权限
		}
	}

	//账号（任意类型）被禁用
	@Override
	public void getRcmPermissionNoPermission() {
		RcmPermissionHelper.getRcmPermissionNoPermission(this);
	}

	//用户类型为母账户
	@Override
	public void getRcmPermissionIsParentAccount() {
		RcmPermissionHelper.getRcmPermissionIsParentAccount(this);
	}

	//注册时填写公司编号，被母账号拒绝
	@Override
	public void getRcmPermissionNoBindCompany() {
		RcmPermissionHelper.getRcmPermissionNoBindCompany(this);
	}

	//注册时填写公司编号，母账号未处理申请
	@Override
	public void getRcmPermissionApplying() {
		RcmPermissionHelper.getRcmPermissionApplying(this);
	}

	//公司编号过期
	@Override
	public void getRcmPermissionChannelExpired(final String company, final String phone) {
		RcmPermissionHelper.getRcmPermissionChannelExpired(this, company, phone);
	}

	//账户未经过实名认证
	@Override
	public void getRcmPermissionNoRealName() {
		RcmPermissionHelper.getRcmPermissionNoRealName(this);
	}

	@Override
	public void getRcmPermissionSuccess() {
		RcmPermissionHelper.getRcmPermissionSuccess(this);
	}


	@Override
	public void getRecommendPassFailure(String tips) {
		RcmPermissionHelper.getRecommendPassFailure(this, tips);
	}

	@Override
	public void getLASShareUrlSuccess(RecommendPermissionModel.ShareBean shareBean) {

	}

	@Override
	public void lasShareNotOpen() {

	}

	@Override
	public void getLasShareUrlFailed(String tips) {

	}

}
